# -*- coding: utf-8 -*-
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtWidgets import QTabWidget


class TabBar(QtWidgets.QTabBar):
    def tabSizeHint(self, index):
        s = super().tabSizeHint(index)
        # 宽度固定，高度适应文字和图标
        return QtCore.QSize(67, 100)

    def paintEvent(self, event):
        painter = QtWidgets.QStylePainter(self)
        opt = QtWidgets.QStyleOptionTab()

        for i in range(self.count()):
            self.initStyleOption(opt, i)
            rect = self.tabRect(i)

            # 先画形状
            painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, opt)

            painter.save()

            # 绘制图标（如果有）
            icon = self.tabIcon(i)
            iconSize = QtCore.QSize(24, 24)
            if not icon.isNull():
                pix = icon.pixmap(iconSize)
                iconRect = QtCore.QRect(0, 0, pix.width(), pix.height())
                iconRect.moveCenter(QtCore.QPoint(rect.center().x(), rect.top() + pix.height()//2 + 4))
                painter.drawPixmap(iconRect.topLeft(), pix)

            # 绘制文字水平居中
            textRect = QtCore.QRect(rect)
            textRect.setTop(rect.top() + 30)  # 顶部留出图标空间
            painter.drawText(textRect, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop, self.tabText(i))

            painter.restore()


class TabWidget(QtWidgets.QTabWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setTabBar(TabBar(self))
        self.setTabPosition(QtWidgets.QTabWidget.West)
        # 可选：设置样式
        self.setStyleSheet("""
        QTabWidget {
            background: #EFE9E4;
        }
        QTabWidget::pane {
            border: 1px solid #D4BFAF;
            background: #F8F4F0;
            border-radius: 6px;
            margin: 2px;
        }
        QTabBar::tab {
            background: qlineargradient(
                x1:0, y1:0, x2:1, y2:1,
                stop:0 #F4ECE5,
                stop:0.45 #EBDFD6,
                stop:1 #DECCBF
            );
            color: #6D4C41;
            border: 1.8px solid #BB9A89;
            border-radius: 7px;
            min-width: 90px;
            min-height: 54px;
            font-size: 9.5pt;
            font-weight: 500;
            padding: 8px 6px 2px;
            margin: 5px 3px;
        }
        QTabBar::tab:selected {
            background: qlineargradient(
                x1:0, y1:0, x2:1, y2:1,
                stop:0 #C5A38F,
                stop:0.45 #AD8874,
                stop:1 #8A6A57
            );
            color: white;
            border-color: #7B5C4C;
            font-weight: 600;
        }
        QTabBar::tab:hover {
            background: qlineargradient(
                x1:0, y1:0, x2:1, y2:1,
                stop:0 #EDE2D9,
                stop:0.45 #E2D3C9,
                stop:1 #D3BDAF
            );
            color: #5B3E34;
            border-color: #A68673;
        }
        QTabBar::tab:disabled {
            background: #F0E8E2;
            color: #B7A79C;
            border-color: #D9C9C1;
        }
        """)

class BaseTabWidget(QTabWidget):
    """
    tabWidget基础类
    """
    def __init__(self, parent=None):
        super().__init__(parent)


class NorthTabWidget(BaseTabWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTabPosition(QTabWidget.TabPosition.North)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = TabWidget()
    w.addTab(QtWidgets.QWidget(), "制造")
    w.addTab(QtWidgets.QWidget(), "品质")
    w.addTab(QtWidgets.QWidget(), "资料")
    w.addTab(QtWidgets.QWidget(), "用户")
    w.show()
    sys.exit(app.exec())
